package com.jinlia.show.modules.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.modules.base.system.entity.SysDict;
import com.jinlia.show.modules.base.model.form.DictForm;
import com.jinlia.show.modules.base.model.po.SysDictPO;
import com.jinlia.show.modules.base.model.query.DictPageQuery;
import com.jinlia.show.modules.base.system.service.SysDictMapperService;
import com.jinlia.show.modules.system.model.Option;
import com.jinlia.show.modules.system.service.ifc.ISysDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysDictService extends SysDictMapperService implements ISysDictService {
    @Override
    public Page<SysDict> getDictPage(DictPageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getCurrent();
        int pageSize = queryParams.getSize();
        String keywords = queryParams.getKeywords();
        String typeCode = queryParams.getTypeCode();

        // 查询数据
        LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysDictLambdaQueryWrapper.like(StringUtils.hasLength(keywords), SysDict::getName, keywords);
        sysDictLambdaQueryWrapper.eq(StringUtils.hasLength(typeCode), SysDict::getTypeCode, typeCode);
        sysDictLambdaQueryWrapper.select(SysDict::getId, SysDict::getName, SysDict::getValue, SysDict::getStatus);
        Page<SysDict> dictItemPage = this.page(new Page<>(pageNum, pageSize), sysDictLambdaQueryWrapper);
//        List<SysDictPO> collect = dictItemPage.getRecords().stream().map(SysDictPO::fromEntity).collect(Collectors.toList());
//        // 实体转换
//        dictItemPage.setRecords(SysDictPO)
        return dictItemPage;
    }

    @Override
    public SysDictPO getDictForm(Long id) {
        // 获取entity
        SysDict entity = this.getOne(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getId, id)
                .select(
                        SysDict::getId,
                        SysDict::getTypeCode,
                        SysDict::getName,
                        SysDict::getValue,
                        SysDict::getStatus,
                        SysDict::getSort,
                        SysDict::getRemark
                ));

        // 实体转换
//        DictForm dictForm = dictConverter.entity2Form(entity);
        SysDictPO sysDictVO = SysDictPO.fromEntity(entity);
        return sysDictVO;
    }

    @Override
    public boolean saveDict(DictForm dictForm) {
        // 实体对象转换 form->entity
        SysDict entity = new SysDict();
        BeanUtils.copyProperties(dictForm, entity);
        boolean result = this.save(entity);
        return result;
    }

    @Override
    public boolean updateDict(Long id, DictForm dictForm) {
        SysDict entity = new SysDict();
        BeanUtils.copyProperties(dictForm, entity);
//        SysDict entity = dictConverter.form2Entity(dictForm);
        boolean result = this.updateById(entity);
        return result;
    }

    @Override
    public boolean deleteDict(String idsStr) {
        //
        List<Long> ids = Arrays.asList(idsStr.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());

        // 删除字典数据项
        boolean result = this.removeByIds(ids);
        return result;
    }

    @Override
    public List<Option> listDictOptions(String typeCode) {
        // 数据字典项
        List<SysDict> dictList = this.list(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getTypeCode, typeCode)
                .select(SysDict::getValue, SysDict::getName)
        );
        // 转换下拉数据
        List<Option> options = dictList
                .stream()
                .map(dictItem -> new Option(dictItem.getValue(), dictItem.getName()))
                .collect(Collectors.toList());
        return options;
    }
}
